package cn.test.controller;

import cn.test.bean.DataVO;
import cn.test.bean.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class DataController {


	@RequestMapping(value = "/data", method = RequestMethod.GET)
	@ResponseBody
	public DataVO getData() {
		DataVO result = new DataVO();
		result.setName("123");
		result.setAge(11);
		result.setDes("介绍");
		return result;
	}
	
	@RequestMapping(value = "/recdata", method = RequestMethod.POST)
	@ResponseBody
	public PageBean recData( HttpServletRequest request) throws UnsupportedEncodingException, IOException {
		InputStream is = request.getInputStream();
		if (is != null) {
			// 获取微信消息
			StringBuilder xmlMsg = new StringBuilder();
			byte[] b = new byte[4096];
			for (int n; (n = is.read(b)) != -1;) {
				xmlMsg.append(new String(b, 0, n, "UTF-8"));
			}
			System.out.println(xmlMsg);
		}
		return null;
	}

	@RequestMapping(value = "/data", method = RequestMethod.POST)
	@ResponseBody
	public Map getData(@RequestBody DataVO vo) {
		System.out.println(vo.getName());

		Map map = new HashMap();
		map.put("name", "test");
		map.put("age", 17);
		return map;
	}

	@RequestMapping("/data/view")
	public String getEmployeeNegotiating(ModelMap model) {
		DataVO result = new DataVO();
		result.setName("123");
		result.setAge(11);
		result.setDes("介绍");
		
		DataVO r = new DataVO();
		r.setName("321");
		r.setAge(12);
		r.setDes("介绍12");
		model.addAttribute("ccc", r);
		model.addAttribute("data", result);
		return "employee";
	}
}
